<template>
  <nav class="navbar" role="navigation" aria-label="主导航">
    <div class="container">
      <router-link to="/" class="logo" aria-label="返回首页">
        <div class="logo-icon" aria-hidden="true">🤖</div>
        <span class="logo-text">AI小课堂</span>
      </router-link>
      
      <div class="nav-links" role="menubar">
        <router-link to="/" class="nav-link" role="menuitem">首页</router-link>
        <a href="/#technologies" class="nav-link" role="menuitem" @click.prevent="navigateToSection('technologies')">技术领域</a>
        <router-link to="/guide" class="nav-link" role="menuitem">AI转变指南</router-link>
        <router-link to="/about" class="nav-link" role="menuitem">关于</router-link>
      </div>
      
      <button 
        class="menu-toggle" 
        @click="toggleMenu"
        :aria-expanded="menuOpen"
        aria-label="切换导航菜单"
        aria-controls="mobile-menu"
      >
        <span class="sr-only">{{ menuOpen ? '关闭' : '打开' }}菜单</span>
        <span aria-hidden="true"></span>
        <span aria-hidden="true"></span>
        <span aria-hidden="true"></span>
      </button>
    </div>
    
    <div 
      id="mobile-menu"
      class="mobile-menu" 
      :class="{ active: menuOpen }"
      role="menu"
      :aria-hidden="!menuOpen"
    >
      <router-link to="/" class="mobile-link" @click="closeMenu" role="menuitem">首页</router-link>
      <a href="/#technologies" class="mobile-link" @click.prevent="navigateToSectionMobile('technologies')" role="menuitem">技术领域</a>
      <router-link to="/guide" class="mobile-link" @click="closeMenu" role="menuitem">AI转变指南</router-link>
      <router-link to="/about" class="mobile-link" @click="closeMenu" role="menuitem">关于</router-link>
    </div>
  </nav>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'

const router = useRouter()
const route = useRoute()
const menuOpen = ref(false)

const toggleMenu = () => {
  menuOpen.value = !menuOpen.value
}

const closeMenu = () => {
  menuOpen.value = false
}

// 导航到指定的锚点区域
const navigateToSection = async (sectionId) => {
  // 如果当前不在首页，先跳转到首页
  if (route.path !== '/') {
    await router.push(`/#${sectionId}`)
  } else {
    // 如果已经在首页，直接滚动到指定区域
    const element = document.getElementById(sectionId)
    if (element) {
      element.scrollIntoView({ behavior: 'smooth', block: 'start' })
    }
  }
}

// 移动端导航（需要先关闭菜单）
const navigateToSectionMobile = async (sectionId) => {
  closeMenu()
  await navigateToSection(sectionId)
}
</script>

<style scoped>
.navbar {
  background: rgba(26, 31, 53, 0.7);
  backdrop-filter: blur(20px) saturate(180%);
  border-bottom: 1px solid rgba(99, 102, 241, 0.2);
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  box-shadow: var(--shadow-lg);
  transition: all var(--duration-normal) ease;
}

.navbar::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: var(--gradient-accent);
  background-size: 200% 100%;
  animation: gradient-flow 3s ease infinite;
}

@keyframes border-flow {
  0%, 100% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: var(--space-sm) var(--space-lg);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  font-size: var(--font-xl);
  font-weight: 700;
  transition: all var(--duration-normal) ease;
  position: relative;
}

.logo:hover {
  transform: scale(1.05);
  filter: drop-shadow(0 0 10px rgba(99, 102, 241, 0.5));
}

.logo-icon {
  font-size: 2rem;
  animation: float 3s ease-in-out infinite;
  filter: drop-shadow(0 0 10px rgba(255, 255, 255, 0.3));
  transition: all 0.3s ease;
}

.logo:hover .logo-icon {
  animation: float 1s ease-in-out infinite, rotate-slow 3s linear infinite;
}

@keyframes rotate-slow {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.logo-text {
  background: var(--gradient-accent);
  background-size: 200% 200%;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  animation: gradient-flow 3s ease infinite;
}

/* Usar gradient-flow de style.css */

.nav-links {
  display: flex;
  gap: 2rem;
}

.nav-link {
  color: var(--text-secondary);
  font-weight: 500;
  transition: all 0.3s ease;
  position: relative;
  padding: 0.5rem 0;
}

.nav-link::before {
  content: '';
  position: absolute;
  bottom: -5px;
  left: 50%;
  width: 0;
  height: 2px;
  background: linear-gradient(90deg, var(--neon-blue), var(--neon-purple));
  transform: translateX(-50%);
  transition: width 0.3s ease;
  box-shadow: 0 0 10px var(--neon-blue);
}

.nav-link::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  background: radial-gradient(circle, rgba(99, 102, 241, 0.2), transparent);
  transform: translate(-50%, -50%);
  border-radius: 50%;
  transition: all 0.3s ease;
  z-index: -1;
}

.nav-link:hover {
  color: var(--neon-blue);
  text-shadow: 0 0 10px rgba(0, 243, 255, 0.5);
}

.nav-link:hover::before {
  width: 100%;
}

.nav-link:hover::after {
  width: 120%;
  height: 200%;
}

.menu-toggle {
  display: none;
  flex-direction: column;
  gap: 4px;
  background: transparent;
  padding: 0.5rem;
}

.menu-toggle span {
  width: 25px;
  height: 3px;
  background: var(--text-primary);
  border-radius: 2px;
  transition: all 0.3s ease;
}

.mobile-menu {
  display: none;
  flex-direction: column;
  background: var(--dark-surface);
  border-top: 1px solid var(--dark-border);
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease;
}

.mobile-menu.active {
  max-height: 300px;
}

.mobile-link {
  padding: 1rem 2rem;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--dark-border);
  transition: all 0.3s ease;
}

.mobile-link:hover {
  color: var(--text-primary);
  background: rgba(99, 102, 241, 0.1);
}

@media (max-width: 768px) {
  .nav-links {
    display: none;
  }
  
  .menu-toggle {
    display: flex;
  }
  
  .mobile-menu {
    display: flex;
  }
}
</style>

